package oceanlife.psychmodel;

import java.util.Random;

import oceanlife.RandomGen;

public class Ocean {
	
	private double O;
	private double C;
	private double E;
	private double A;
	private double N;
	
	
	//ocean parameters are distributed over a normal distribution
	public Ocean(){
		double[][] CorMat = getCorMatOCEAN();
		double[] SD = {0.64, 0.53, 0.48, 0.46, 0.52};
		double[] mean = {2.77, 3.64, 3.55, 3.43, 3.74};
        double[][] covMat  = corToCov(CorMat,SD);
        double[][] CC = Cholesky.cholesky(covMat);
        copyLowerLeftToUpperRight(CC);
        for (int i = 0; i < SD.length; i++) {
            for (int j = 0; j < SD.length; j++) {
                //System.out.printf("%8.5f ", CC[i][j]);
            }
            //System.out.println();
        }

        double[] rawGens = new double[SD.length];
        for(int i = 0; i < SD.length;i++){
        	//rawGens[i] = RandomGen.getGaussian();
        	rawGens[i] = 0;//RandomGen.getRand(0, 1);;
        }
		
        double[] correlatedGens = new double[SD.length];
        for(int i = 0;i<SD.length;i++){
        	correlatedGens[i] = mean[i];
        	for(int j = 0; j < SD.length;j++){
        		correlatedGens[i] += CC[i][j]*rawGens[j];
        	}
        }
        //OCEAN atributes are mean 0 and SD 1
		N = (correlatedGens[0]-mean[0])/SD[0];
		E = (correlatedGens[1]-mean[1])/SD[1];
		O = (correlatedGens[2]-mean[2])/SD[2];
		A = (correlatedGens[3]-mean[3])/SD[3];
		C = (correlatedGens[4]-mean[4])/SD[4];
		
		//for(int i = 0 ; i < SD.length;i++){
			//if(correlatedGens[i] > 5.0 || correlatedGens[i] < 1.0){
				System.out.println("N="+N+" E="+E+" O="+O+" A="+A+" C="+C);
			//}
		//}

		
	}
	
	private void copyLowerLeftToUpperRight(double[][] cC) {
		for (int i = 0; i < cC.length;i++){
			for(int j = i; j >= 0; j--){
				cC[j][i] = cC[i][j];
			}
		}
		
	}

	private double[][] corToCov(double[][] corMat, double[] sD) {
		double[][] covMat = new double[sD.length][sD.length];
//		for(int i = 0;i < sD.length;i++){
//			covMat[i] = 
//		}
		for(int i = 0; i < sD.length;i++){
			for(int j = 0; j < sD.length;j++){
				covMat[i][j] = corMat[i][j]*sD[i]*sD[j];
			}
		}
		return covMat;
	}

	public double[][] getCorMatOCEAN(){
		double[][] A = { { 1, -0.25, -0.23, -0.20, -0.38  },
                { -0.25, 1,  0.43, 0.14, 0.29 },
                { -0.23, 0.43, 1, 0.06, 0.32 },
                { -0.20, 0.14, 0.06, 1, 0.03 },
                { -0.38, 0.29, 0.32, 0.03, 1 }
              };
		return A;
	}
	
	public double initSelfControl() {
		// TODO Auto-generated method stub
		return 0;
	}

	public double initGoalCongruence() {
		// TODO Auto-generated method stub
		return 0;
	}

	public double initPerformance() {
		// TODO Auto-generated method stub
		return 0;
	}

	public double initPassiveEndurance() {
		// TODO Auto-generated method stub
		return 0;
	}

	public double initEmotionalRegulation() {
		// TODO Auto-generated method stub
		return 0;
	}

	
	public static void main(String[] args){
		for(int i = 0;i<20;i++){
			Ocean plub = new Ocean();
			if(i % 10000 == 0){
				System.out.println(i);
			}
		}
	}

	public double initCompassionReg() {
		// TODO Auto-generated method stub
		return 0;
	}

	public double initHopeReg() {
		// TODO Auto-generated method stub
		return 0;
	}

	public double initAnxietyReg() {
		// TODO Auto-generated method stub
		return 0;
	}

	public double initFearReg() {
		// TODO Auto-generated method stub
		return 0;
	}
	
}
